package cn.zongjie.day05;

import java.math.BigDecimal;

public class Demo4 {
    public static void main(String[] args) {
        // 求以下double数组的平均值(四舍五入保留两位小数)：
        double[] arr1 = {0.1, 0.2, 2.1, 3.2, 5.56, 7.21};
        String[] arr2 = new String[arr1.length];
        for (int i = 0; i < arr1.length; i++) {
            arr2[i] = String.valueOf(arr1[i]);
        }
        BigDecimal bd1 = new BigDecimal(arr2[0]);
        BigDecimal bd2 = new BigDecimal(arr2[1]);
        BigDecimal bd3 = new BigDecimal(arr2[2]);
        BigDecimal bd4 = new BigDecimal(arr2[3]);
        BigDecimal bd5 = new BigDecimal(arr2[4]);
        BigDecimal bd6 = new BigDecimal(arr2[5]);
        BigDecimal sum = bd1.add(bd2).add(bd3).add(bd4).add(bd5).add(bd6);
        System.out.println("和为" + sum);
        BigDecimal length = new BigDecimal(arr1.length);
        System.out.println("平均值为"+sum.divide(length, 2, BigDecimal.ROUND_HALF_UP));

    }
}
